package coursecase;

import java.io.*;
import java.util.Scanner;

//实现文件复制
public class Test2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要复制的文件所在的路径:");

        File souFile = new File(scanner.nextLine());
        if (!souFile.isFile()) {
            System.out.println("你输入的不是一个文件路径，请重新输入!");
            return;
        }

        System.out.print("请输入要复制到的目标路径:");
        File desFile = new File(scanner.nextLine());
        if (desFile.exists()) {
            if (!desFile.isFile()) {
                System.out.print("输入的不是一个文件路径，请重新输入!");
                return;
            }else {
                System.out.print("文件已经存在，是否覆盖？ y/n");
                String s = scanner.next();
                if (!s.toLowerCase().equals("y")){
                    System.out.print("停止复制");
                    return;
                }
            }
        }
        try(InputStream inputStream = new FileInputStream(souFile)){
            try(OutputStream outputStream = new FileOutputStream(desFile)) {
                byte[] bytes = new byte[1024];
                int len;
                while (true) {
                    len = inputStream.read(bytes);
                    if (len == -1) break;
                    outputStream.write(bytes,0,len);
                }
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            System.out.println("复制完成");
        }
    }
}
